package com.yuncheng.spcyApi.controller;

import org.apache.commons.lang3.StringUtils;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.busService.SpcyDaService;
import com.yuncheng.spcyApi.constant.other.DaConstant;
import com.yuncheng.spcyApi.entity.SpcyDaDzdak;
import com.yuncheng.spcyApi.entity.SpcyDaSqlc;
import com.yuncheng.spcyApi.entity.SpcyDaZzdak;
import com.yuncheng.exception.BusinessException;
import com.yuncheng.vo.HttpResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description: 审评查验-档案管理相关信息
 * @Author: csx
 * @Date: 2023-03-03
 * @Version: V1.0
 */
/*@Api(tags = "审评查验-档案管理相关信息")
@RestController
@RequestMapping("/api/spzx-spcy/spcyDaInfo")*/
public class SpcyDaController {

    private static final Logger log = LoggerFactory.getLogger(SpcyDaController.class);

    @Resource
    private SpcyDaService spcyDaService;


    @Log(title = "查询电子档案-分页查询-管理层面")
    @ApiOperation(value = "查询电子档案-分页查询-管理层面")
    @GetMapping(value = "queryDzdaPageList")
    public HttpResult queryDzdaPageList(SpcyDaDzdak dzdak,
                                        @RequestParam(value = "pageNo",defaultValue = "1")Integer pageNo,
                                        @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        return spcyDaService.queryDzdaPageList(dzdak,pageNo,pageSize);
    }

    @Log(title = "查询电子档案-分页查询-公开等级")
    @ApiOperation(value = "查询电子档案-分页查询-公开等级")
    @GetMapping(value = "queryDzdaGkPageList")
    public HttpResult queryDzdaGkPageList(SpcyDaDzdak dzdak,
                                        @RequestParam(value = "pageNo",defaultValue = "1")Integer pageNo,
                                        @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        dzdak.setFbmdj(DaConstant.DA_BMDJ_GK);
        dzdak.setFsfgd(DaConstant.DZDA_GDZT_YES);
        return spcyDaService.queryDzdaPageList(dzdak,pageNo,pageSize);
    }

    @Log(title = "查询纸质档案-分页查询-管理层面")
    @ApiOperation(value = "查询纸质档案-分页查询-管理层面")
    @GetMapping(value = "queryZzdaPageList")
    public HttpResult queryZzdaPageList(SpcyDaZzdak zzdak,
                                        @RequestParam(value = "pageNo",defaultValue = "1")Integer pageNo,
                                        @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        return spcyDaService.queryZzdaPageList(zzdak,pageNo,pageSize);
    }

    @Log(title = "查询纸质档案-分页查询-公开等级")
    @ApiOperation(value = "查询纸质档案-分页查询-公开等级")
    @GetMapping(value = "queryZzdaGkPageList")
    public HttpResult queryZzdaGkPageList(SpcyDaZzdak zzdak,
                                        @RequestParam(value = "pageNo",defaultValue = "1")Integer pageNo,
                                        @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize){
        zzdak.setFbmdj(DaConstant.DA_BMDJ_GK);
        zzdak.setFrkzt(DaConstant.ZZDA_RKZT_YJRK);
        return spcyDaService.queryZzdaPageList(zzdak,pageNo,pageSize);
    }

    @Log(title = "查询借阅档案-分页查询-管理层面")
    @ApiOperation(value = "查询借阅档案-分页查询-管理层面")
    @GetMapping(value = "daSqlcPageList")
    public HttpResult daSqlcPageList(SpcyDaSqlc sqlc,
                                     @RequestParam(value = "pageNo",defaultValue = "1")Integer pageNo,
                                     @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                                     @RequestParam(value = "onlyCurrentUser",defaultValue = "false") boolean onlyCurrentUser){
        return spcyDaService.queryDaSqlcPageList(sqlc,pageNo,pageSize,onlyCurrentUser);
    }

    @Log(title = "查询借阅档案-分页查询-个人层面")
    @ApiOperation(value = "查询借阅档案-分页查询-个人层面")
    @GetMapping(value = "daSqlcMyPageList")
    public HttpResult daSqlcMyPageList(SpcyDaSqlc sqlc,
                                     @RequestParam(value = "pageNo",defaultValue = "1")Integer pageNo,
                                     @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                                     @RequestParam(value = "onlyCurrentUser",defaultValue = "true") boolean onlyCurrentUser){
        return spcyDaService.queryDaSqlcPageList(sqlc,pageNo,pageSize,onlyCurrentUser);
    }

    @Log(title = "添加电子档案")
    @ApiOperation(value = "添加电子档案")
    @PostMapping(value = "addDzda")
    public HttpResult addDzda(@RequestBody SpcyDaDzdak dzdak){
        if (StringUtils.isBlank(dzdak.getId())) {
            return spcyDaService.addDzda(dzdak);
        }else {
            try {
                return spcyDaService.updateDzda((HashMap<String, String>)BeanUtils.describe(dzdak));
            } catch (InvocationTargetException e) {
                log.error("【InvocationTargetException】保存出错：{}",e.getMessage());
                throw new BusinessException("【InvocationTargetException】保存出错："+e.getMessage());
            } catch (IllegalAccessException e) {
                log.error("【IllegalAccessException】保存出错：{}",e.getMessage());
                throw new BusinessException("【IllegalAccessException】保存出错："+e.getMessage());
            } catch (NoSuchMethodException e) {
                log.error("【NoSuchMethodException】保存出错：{}",e.getMessage());
                throw new BusinessException("【NoSuchMethodException】保存出错："+e.getMessage());
            }
        }
    }

    @Log(title = "添加纸质档案")
    @ApiOperation(value = "添加纸质档案")
    @PostMapping(value = "addZzda")
    public HttpResult addZzda(@RequestBody SpcyDaZzdak zzdak){
        if (StringUtils.isBlank(zzdak.getId())) {
            return spcyDaService.addZzda(zzdak);
        }else {
            try {
                return spcyDaService.updateZzda((HashMap<String, String>)BeanUtils.describe(zzdak));
            } catch (InvocationTargetException e) {
                log.error("【InvocationTargetException】保存出错：{}",e.getMessage());
                throw new BusinessException("【InvocationTargetException】保存出错："+e.getMessage());
            } catch (IllegalAccessException e) {
                log.error("【IllegalAccessException】保存出错：{}",e.getMessage());
                throw new BusinessException("【IllegalAccessException】保存出错："+e.getMessage());
            } catch (NoSuchMethodException e) {
                log.error("【NoSuchMethodException】保存出错：{}",e.getMessage());
                throw new BusinessException("【NoSuchMethodException】保存出错："+e.getMessage());
            }
        }
    }

    @Log(title = "更新电子档案")
    @ApiOperation(value = "更新电子档案")
    @PutMapping(value = "updateDzda")
    public HttpResult updateDzda(@RequestBody HashMap<String, String> buffer){
        try {
            return spcyDaService.updateDzda(buffer);
        } catch (InvocationTargetException e) {
            log.error("【InvocationTargetException】保存出错：{}",e.getMessage());
            throw new BusinessException("【InvocationTargetException】保存出错："+e.getMessage());
        } catch (IllegalAccessException e) {
            log.error("【IllegalAccessException】保存出错：{}",e.getMessage());
            throw new BusinessException("【IllegalAccessException】保存出错："+e.getMessage());
        }
    }

    @Log(title = "更新纸质档案")
    @ApiOperation(value = "更新纸质档案")
    @PutMapping(value = "updateZzda")
    public HttpResult updateZzda(@RequestBody HashMap<String, String> buffer){
        try {
            return spcyDaService.updateZzda(buffer);
        } catch (InvocationTargetException e) {
            log.error("【InvocationTargetException】保存出错：{}",e.getMessage());
            throw new BusinessException("【InvocationTargetException】保存出错："+e.getMessage());
        } catch (IllegalAccessException e) {
            log.error("【IllegalAccessException】保存出错：{}",e.getMessage());
            throw new BusinessException("【IllegalAccessException】保存出错："+e.getMessage());
        }
    }

    @Log(title = "删除电子档案")
    @ApiOperation(value = "删除电子档案")
    @ApiImplicitParam(name = "ids",value = "档案id,多个英文逗号分割")
    @DeleteMapping(value = "deleteDzda")
    public HttpResult deleteDzda(@RequestParam(value = "ids",defaultValue = "")String ids){
        return spcyDaService.deleteDzda(ids);
    }

    @Log(title = "删除纸质档案")
    @ApiOperation(value = "删除纸质档案")
    @ApiImplicitParam(name = "ids",value = "档案id,多个英文逗号分割")
    @DeleteMapping(value = "deleteZzda")
    public HttpResult deleteZzda(@RequestParam(value = "ids",defaultValue = "")String ids){
        return spcyDaService.deleteZzda(ids);
    }

    @Log(title = "获取电子档案-通过档案id")
    @ApiOperation(value = "获取电子档案-通过档案id")
    @GetMapping(value = "getDzdaById")
    public HttpResult getDzdaById(@RequestParam(value = "id",defaultValue = "")String id){
        if (StringUtils.isBlank(id)){
            return HttpResult.error("档案参数丢失！请联系管理员！");
        }
        SpcyDaDzdak dzda = spcyDaService.getDzdaById(id);
        return HttpResult.ok(dzda);
    }

    @Log(title = "获取纸质档案-通过档案id")
    @ApiOperation(value = "获取纸质档案-通过档案id")
    @GetMapping(value = "getZzdaById")
    public HttpResult getZzdaById(@RequestParam(value = "id",defaultValue = "")String id){
        if (StringUtils.isBlank(id)){
            return HttpResult.error("档案参数丢失！请联系管理员！");
        }
        SpcyDaZzdak zzda = spcyDaService.getZzdaById(id);
        return HttpResult.ok(zzda);
    }

    @Log(title = "电子档案归档-可批量")
    @ApiOperation(value = "电子档案归档-可批量")
    @ApiImplicitParam(name = "ids",value = "档案id,多个英文逗号分割")
    @PostMapping(value = "gdDzda")
    public HttpResult gdDzda(@RequestBody Map<String,String> map){
        String ids = map.get("ids");
        return spcyDaService.gdDzda(ids);
    }

    @Log(title = "纸质档案入库-可批量")
    @ApiOperation(value = "纸质档案入库-可批量")
    @ApiImplicitParam(name = "ids",value = "档案id,多个英文逗号分割")
    @PostMapping(value = "rkZzda")
    public HttpResult rkZzda(@RequestBody Map<String,String> map){
        String ids = map.get("ids");
        return spcyDaService.rkZzda(ids,null);
    }

    @Log(title = "上传电子档案文件")
    @ApiOperation(value = "上传电子档案文件")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "fdaid",value = "档案id")
    })
    @PostMapping(value = "uploadMoreTypeFile")
    public HttpResult uploadMoreTypeFile(@RequestParam("file")MultipartFile file,@RequestParam(value = "fdaid",defaultValue = "")String fdaid){
        return spcyDaService.uploadMoreTypeFile(file,fdaid);
    }

    @Log(title = "下载电子档案文件-根据档案id")
    @ApiOperation(value = "下载电子档案文件-根据档案id")
    @ApiImplicitParam(name = "fdaid",value = "档案id")
    @GetMapping(value = "getLocalFileById")
    public void getLocalFileById(@RequestParam(value = "fdaid",defaultValue = "") String fdaid, HttpServletResponse response){
        spcyDaService.getLocalFileByDaId(fdaid, response);
    }

    @Log(title = "获取档案文件类型-根据档案id")
    @ApiOperation(value = "获取档案文件类型-根据档案id")
    @ApiImplicitParam(name = "fdaid",value = "档案id")
    @GetMapping(value = "getFileType")
    public HttpResult getFileType(@RequestParam(value = "fdaid",defaultValue = "") String fdaid){
        return spcyDaService.getFileTypeByDaId(fdaid);
    }

    @Log(title = "确认归还-纸质档案")
    @ApiOperation(value = "确认归还-纸质档案")
    @ApiImplicitParam(name = "fdaid",value = "档案id")
    @GetMapping(value = "ghzzDa")
    public HttpResult ghzzDa(@RequestParam(value = "fdaid",defaultValue = "") String fdaid){
        return spcyDaService.ghzzDa(fdaid,null);
    }

    /*@Log(title = "借出纸质档案")
    @ApiOperation(value = "借出纸质档案")
    @GetMapping(value = "jczzDa")*/
    public HttpResult jczzDa(@RequestParam(value = "fdaid",defaultValue = "") String fdaid){
        return spcyDaService.jczzDa(fdaid,null);
    }







}
